

#import "../sleipner_config.h"

#ifndef _PLATFORM_IOS

#import <Cocoa/Cocoa.h>
#import "NSCursorWrappers.h"

//	From "Cocoa Drawing Guide: Working with Images"
NSImage	*CreateNSImageFromCGImage( CGImageRef image )
{
	NSRect			imageRect		= NSMakeRect(0.0, 0.0, 0.0, 0.0);

	// Get the image dimensions.
	imageRect.size.height = CGImageGetHeight(image);
	imageRect.size.width = CGImageGetWidth(image);

	// Create a new image to receive the Quartz image data.
	NSImage	*newImage = [[NSImage alloc] initWithSize:imageRect.size]; 
	[newImage lockFocus];

	// Get the Quartz context and draw.
	CGContextRef	imageContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
	CGContextDrawImage( imageContext, *(CGRect*)&imageRect, image );
	[newImage unlockFocus];

	return( newImage );
}

//	C-based style wrapper routines around NSCursor
CursorRef	CreateCocoaCursor( CGImageRef cgImageRef, float hotSpotX, float hotSpotY )
{
	static BOOL		firstTime	= YES;
	
	if ( firstTime )
	{
		//	Must first call [[[NSWindow alloc] init] release] to get the NSWindow machinery set up so that NSCursor can use a window to cache the cursor image
		[[[NSWindow alloc] init] release];
		firstTime = NO;
	}
	
	NSImage		*nsImage	= CreateNSImageFromCGImage( cgImageRef );
	NSCursor	*cursor		= [[NSCursor alloc] initWithImage:nsImage hotSpot:NSMakePoint( hotSpotX, hotSpotY )];
	
	[nsImage release];
	
	return( (CursorRef)cursor );
}

void	ReleaseCocoaCursor( CursorRef cursor )
{
	[(NSCursor *)cursor release];
}

void	SetCocoaCursor( CursorRef cursor )
{
	[(NSCursor *)cursor set];
}

void	HideCocoaCursor()
{
	[NSCursor hide];
}

void	ShowCocoaCursor()
{
	[NSCursor unhide];
}

#endif

